package http;

import okhttp3.*;

import java.util.concurrent.TimeUnit;

public class BasicHttpClient {
    private OkHttpClient defaultClient;
    private OkHttpClient preInitClient;
    private OkHttpClient robInitClient;
    //调整超时时间 免得线程过多的时候 或线路阻塞时候
    //服务端处理不及时 导致超时异常
    /**毫秒单位
     * 读超时是考虑服务端的情况,包已经发送过去了,结果我可以不管它,避免过多线程阻塞后续发包
     * 建立链接考虑4秒左右 行就行 不行就再来
     * 写超时是考虑客户端的 一般问题不大 这里不大可能会丢包 相反是读超时阻碍了时间*/
    /**资源初始化的时候尽量读超时毫秒要加大 避免失败*/
    /***/

//    final static CookieJar cookieJar = new CookieJar() {
//        //cache
//            private final Map<String, List<Cookie>> cookiesMap = new HashMap<String, List<Cookie>>();
//        private int x = 0;
//
//        @Override
//        synchronized
//        public void saveFromResponse(HttpUrl httpUrl, List<Cookie> cookies) {
////            robClass.logger.info("这是cookies"+cookies);
////            String host = httpUrl.host();
////            if (x > robClass.num) {
////                x = 0;
////            }
//            String tag;
//            robClass.logger.info("httpUrl.uri().toString():" + httpUrl.uri().toString());
//            if (httpUrl.uri().toString().contains("xm=")) {
//                tag = httpUrl.uri().toString().split("xh=")[1].split("&")[0];
//                robClass.logger.info("tag1:" + tag);
//            } else {
//                tag = httpUrl.uri().toString().split("xh=")[1];
//                robClass.logger.info("tag2:" + tag);
//            }
//
////            robClass.logger.info("查看传入tag" + tag);
////            List<Cookie> cookiesList = cookiesMap.get(tag);
////            robClass.logger.info("保存的tag" + tag + "保存的cookiesList" +  cookiesList);
////            if (cookiesList != null) {
////                robClass.logger.info("存在保存的tag" + tag + "存在保存的cookiesList" +  cookiesList);
////                cookiesMap.remove(tag);
////            }
//            cookiesMap.put(tag, cookies);
//            robClass.logger.info("保存的tag" + tag + "保存的cookies" + cookies);
//            robClass.logger.info("saveFromResponse-cookiesMap" + cookiesMap);
//        }
//
//        @Override
//        synchronized
//        public List<Cookie> loadForRequest(HttpUrl httpUrl) {
//
////            robClass.logger.info("httpUrl.host()"+httpUrl.host());
////            robClass.logger.info(httpUrl.uri().toString());
////            robClass.logger.info(httpUrl.uri().toString().split("xh="));
////            robClass.logger.info(httpUrl.uri().toString().split("xh=")[1]);
////            Random random = new Random();
////            int i = random.nextInt(robClass.num);
//            robClass.logger.info("loadForRequest" + httpUrl.url().toString());
//            robClass.logger.info("loadForRequest" + httpUrl.uri().toString());
//            String tag = null;
////            if (httpUrl.uri().toString().contains("xh=") ) {
////                tag = httpUrl.uri().toString().split("xh=")[1].split("&")[0] + "&" + i;
////            } else {
//                tag =  httpUrl.uri().toString();
////            }
//
//            robClass.logger.info("loadForRequest读取的tag" + tag);
//
//            List<Cookie> cookiesList = cookiesMap.get(tag);
//            robClass.logger.info("读取的cookiesList" + cookiesList);
//            robClass.logger.info("cookies" + cookiesMap);
////            robClass.logger.info("cookies"+ cookiesList);
//            return cookiesList != null ? cookiesList : new ArrayList<Cookie>();
//        }
//    };

    public BasicHttpClient() {
        if (defaultClient == null) {
            defaultClient = new OkHttpClient()
                    .newBuilder()
                    .followRedirects(false)
                    .followSslRedirects(false)
                    .readTimeout(3, TimeUnit.SECONDS)
                    .writeTimeout(3, TimeUnit.SECONDS)
                    .connectTimeout(3, TimeUnit.SECONDS)
                    .build();
            defaultClient.dispatcher().setMaxRequests(300);
            defaultClient.dispatcher().setMaxRequestsPerHost(300);
        }
        /**初始化资源发包*/
        if (preInitClient == null) {
            preInitClient = new OkHttpClient()
                    .newBuilder()
                    .followRedirects(false)
                    .followSslRedirects(false)
                    .readTimeout(3, TimeUnit.SECONDS)
                    .writeTimeout(3, TimeUnit.SECONDS)
                    .connectTimeout(3, TimeUnit.SECONDS)
                    .build();
            preInitClient.dispatcher().setMaxRequests(300);
            preInitClient.dispatcher().setMaxRequestsPerHost(300);
        }
        /**狂暴模式发包*/
        if (robInitClient == null) {
            robInitClient = new OkHttpClient()
                    .newBuilder()
                    .followRedirects(false)
                    .followSslRedirects(false)
                    .readTimeout(10, TimeUnit.MILLISECONDS)
                    .writeTimeout(500, TimeUnit.MILLISECONDS)
                    .connectTimeout(1500, TimeUnit.MILLISECONDS)
                    .build();
            robInitClient.dispatcher().setMaxRequests(500);
            robInitClient.dispatcher().setMaxRequestsPerHost(500);
        }
    }

    public OkHttpClient getDefaultClient() {
        return defaultClient;
    }

    public void setDefaultClient(OkHttpClient defaultClient) {
        this.defaultClient = defaultClient;
    }

    public OkHttpClient getPreInitClient() {
        return preInitClient;
    }

    public void setPreInitClient(OkHttpClient preInitClient) {
        this.preInitClient = preInitClient;
    }

    public OkHttpClient getRobInitClient() {
        return robInitClient;
    }

    public void setRobInitClient(OkHttpClient robInitClient) {
        this.robInitClient = robInitClient;
    }
}


